package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.QuaValue;
import com.moody.hibernate.domain.RatioValue;
import com.moody.hibernate.domain.RatioValueId;

/**
 * A data access object (DAO) providing persistence and search support for
 * RatioValue entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.RatioValue
 * @author MyEclipse Persistence Tools
 */

public class RatioValueDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(RatioValueDAO.class);
	// property constants
	public static final String REPORT_ITEM_VALUE = "reportItemValue";
	public static final String ITEM_VALUE = "itemValue";
	public static final String ITEM_WEIGHT = "itemWeight";

	protected void initDao() {
		// do nothing
	}

	public void save(RatioValue transientInstance) {
		log.debug("saving RatioValue instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(RatioValue persistentInstance) {
		log.debug("deleting RatioValue instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RatioValue findById(com.moody.hibernate.domain.RatioValueId id) {
		log.debug("getting RatioValue instance with id: " + id);
		try {
			RatioValue instance = (RatioValue) getHibernateTemplate().get(
					"com.moody.hibernate.domain.RatioValue", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(RatioValue instance) {
		log.debug("finding RatioValue instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding RatioValue instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from RatioValue as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByReportItemValue(Object reportItemValue) {
		return findByProperty(REPORT_ITEM_VALUE, reportItemValue);
	}

	public List findByItemValue(Object itemValue) {
		return findByProperty(ITEM_VALUE, itemValue);
	}

	public List findByItemWeight(Object itemWeight) {
		return findByProperty(ITEM_WEIGHT, itemWeight);
	}

	public List findAll() {
		log.debug("finding all RatioValue instances");
		try {
			String queryString = "from RatioValue";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RatioValue merge(RatioValue detachedInstance) {
		log.debug("merging RatioValue instance");
		try {
			RatioValue result = (RatioValue) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RatioValue instance) {
		log.debug("attaching dirty RatioValue instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(RatioValue instance) {
		log.debug("attaching clean RatioValue instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RatioValueDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RatioValueDAO) ctx.getBean("RatioValueDAO");
	}

	public List<RatioValue> findRatioValueBySerialNoAndModelId(String serialNo,String modelId) {
		String queryString = "from RatioValue as r, ModelInfo as m where r.id.modelId = m.modelId and r.id.reportItemNo like CONCAT('%',m.itemNo) and r.itemValue = m.itemValue and r.id.serialNo = ? and r.id.modelId = ? ORDER BY m.serialNo";
		List list = getHibernateTemplate().find(queryString, serialNo, modelId);
		List<RatioValue> resultList = new ArrayList<RatioValue>();
		for(Object o :list){
			Object[] temp = (Object[]) o;
			resultList.add((RatioValue)temp[0]);
		}
		
		return resultList;
	}

	public void insertRatioValueList(List<RatioValue> ratioValueList) {
		try{
			getHibernateTemplate().saveOrUpdateAll(ratioValueList);
		}catch (Exception e){
			log.error("insert error", e);
		}
	}
}